package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.been.NameDouble;
import com.kaizzi.data.common.been.SeriesData;
import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.xslf.usermodel.XSLFChart;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Slf4j
@Component
public class Template24 {

    @Resource
    private TemplateUtil templateUtil;
    @Resource
    private LogService logService;

    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    if (textRun.getRawText().equals("17KEY-1")){
                        String squareData = data.get("teamUserTitle").asText();
                        templateUtil.text(textParagraph,squareData,false);
                        break;
                    }
                    if (textRun.getRawText().equals("17KEY-3")){
                        String teamData = data.get("typeTitle").asText();
                        templateUtil.text(textParagraph,teamData,false);
                        break;
                    }
                    if (textRun.getRawText().equals("17KEY-5")){
                        String squareData = data.get("statusTitle").asText();
                        templateUtil.text(textParagraph,squareData,false);
                        break;
                    }
                    if (textRun.getRawText().equals("17KEY-7")){
                        String teamData = data.get("demandTitle").asText();
                        templateUtil.text(textParagraph,teamData,false);
                        break;
                    }
                    if (textRun.getRawText().equals("17KEY-9")){
                        String title = data.get("title").asText();
                        templateUtil.text(textParagraph,title,false);
                        break;
                    }
                    if (textRun.getRawText().equals("17KEY-11")){
                        String dataPeriod = data.get("dataPeriod").asText();
                        textRun.setText(dataPeriod);
                        break;
                    }
                }catch (Exception e){
                    logService.setDataLogStatus("ppt-error");
                    log.error("page:{},text Exception:{}",24,e);
                }
            }
        }
    }

    public void barChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        String jsonKey;
        List<SeriesData> barData = null;
        switch (plot.getBarChartList().get(0).getSerList().get(0).getVal().getNumRef().getNumCache().getPtArray(0).getV()){
            case "71":
                jsonKey = "teamTypeCount";
                barData = getBarData2(data,jsonKey);
                break;
            case "22":
                jsonKey = "teamStatusCount";
                barData = getBarData1(data,jsonKey);
                break;
            case "124":
                barData = getSpecialBarData(data);
                break;
        }
        templateUtil.barChart(plot,barData,workbook,sheet,chart);
    }

    private List<SeriesData> getBarData2(JsonNode data, String jsonKey) {
        JsonNode node1 = data.get(jsonKey).get(1).get("beens");
        List<TempBeen> list = getTempBeens(node1);
        List<String> names = new ArrayList<>();
        list.sort(Comparator.comparing(TempBeen::getTempCount).reversed());

        for (TempBeen been : list) {
            names.add(been.getTempName());
        }

        //按名称排序
        List<SeriesData> seriesDatas = new ArrayList<>();
        JsonNode jsonNode = data.get(jsonKey);
        for (JsonNode node : jsonNode) {
            String date = node.get("date").asText();
            JsonNode jsonNode1 = node.get("beens");
            List<NameDouble> nameDoubles = new ArrayList<>();
            for (String name : names) {
                for (JsonNode jsonNode2 : jsonNode1) {
                    if (name.equals(jsonNode2.get("tempName").asText())){
                        NameDouble nameDouble = new NameDouble(jsonNode2.get("tempName").asText(),jsonNode2.get("tempCount").asDouble());
                        nameDoubles.add(nameDouble);
                    }
                }
            }
            SeriesData seriesData = new SeriesData(date,nameDoubles,"");
            seriesDatas.add(seriesData);
        }
        return seriesDatas;
    }

    private List<TempBeen> getTempBeens(JsonNode node) {
        List<TempBeen> beens = new ArrayList<>();
        for (JsonNode jsonNode : node) {
            TempBeen been = new TempBeen();
            been.setTempName(jsonNode.get("tempName").asText());
            been.setTempCount(jsonNode.get("tempCount").asLong());
            beens.add(been);
        }
        return beens;
    }


    private List<SeriesData> getBarData1(JsonNode data,String jsonKey) {
        List<SeriesData> seriesDatas = new ArrayList<>();

        JsonNode jsonNode = data.get(jsonKey);
        for (JsonNode node : jsonNode) {
            String date = node.get("date").asText();
            JsonNode jsonNode1 = node.get("beens");
            List<NameDouble> nameDoubles = new ArrayList<>();
            for (JsonNode jsonNode2 : jsonNode1) {
                NameDouble nameDouble = new NameDouble(jsonNode2.get("tempName").asText(),jsonNode2.get("tempCount").asDouble());
                nameDoubles.add(nameDouble);
            }
            SeriesData seriesData = new SeriesData(date,nameDoubles,"");
            seriesDatas.add(seriesData);
        }
        return seriesDatas;
    }

    private static List<SeriesData> getSpecialBarData(JsonNode jsonNode){
        JsonNode jsonNode1 = jsonNode.get("teamUser");
        List<SeriesData> seriesDatas = new ArrayList<>();
        List<NameDouble> tempDoubles1 = new ArrayList<>();
        List<NameDouble> tempDoubles2 = new ArrayList<>();

        for (JsonNode dateJson:jsonNode1) {
            String date = dateJson.get("date").asText();
            NameDouble tempDouble1 = new NameDouble(date,dateJson.get("newUsers").asDouble());
            NameDouble tempDouble2 = new NameDouble(date,dateJson.get("allUsers").asDouble());
            tempDoubles1.add(tempDouble1);
            tempDoubles2.add(tempDouble2);
        }
        SeriesData tempSeriesData1 = new SeriesData("一键组队中的新用户",tempDoubles1,"2");
        SeriesData tempSeriesData2 = new SeriesData("一键组队参与人数",tempDoubles2,"1");
        seriesDatas.add(tempSeriesData1);
        seriesDatas.add(tempSeriesData2);
        return seriesDatas.stream().sorted(Comparator.comparing(SeriesData::getSort)).collect(Collectors.toList());
    }

    public void pieChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        List<SeriesData> pieData = getPieData(data);
        templateUtil.pieChart(plot,pieData,workbook,sheet,chart);
    }

    private static List<SeriesData> getPieData(JsonNode jsonNode){
        JsonNode pieData = jsonNode.get("teamDemand");
        List<SeriesData> seriesDatas = new ArrayList<>();
        List<NameDouble> tempDouble = new ArrayList<>();

        for (JsonNode dateJson:pieData) {
            NameDouble newUserDouble = new NameDouble(dateJson.get("tempName").asText(),dateJson.get("tempCount").asDouble());
            tempDouble.add(newUserDouble);
        }
        SeriesData newUserSeriesData = new SeriesData("队伍数",tempDouble,"");
        seriesDatas.add(newUserSeriesData);
        return seriesDatas;
    }
}
